package ps.client.gui.timerwindow;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

import ps.client.gui.MainFrame;
import ps.client.gui.util.GblPanel;
import ps.client.gui.util.PrefWindow;
import ps.server.trigger.TriggerEntry;


@SuppressWarnings("serial")
public class TimerWindow extends PrefWindow {
    
    private static Color CONTROLS_PANEL_COLOR_ACTIVE = new Color(80, 80, 80);
    private static Color CONTROLS_PANEL_COLOR_PASSIV = Color.DARK_GRAY;
    
    JPanel contentPanel = new JPanel(new BorderLayout());
    
    JPanel controlsPanel = new JPanel(new BorderLayout());
    JPanel buttonPanel = new JPanel(new GridLayout(1, 1));
    JButton hideButton = new JButton("x");
    JButton biggerButton = new JButton("+");
    JButton smallerButton = new JButton("-");
    JButton clearButton = new JButton("c");
    
    GblPanel outerTimerPanel = new GblPanel();
    
    int currentX = -1;
    int currentY = -1;
    
    boolean visibleIfTimer;
    
    MainFrame mainFrame;
    
    public TimerWindow(MainFrame mainFrame, String prefNode) {
        super(null, prefNode);
        this.mainFrame = mainFrame;
        
        setBackground(Color.BLACK);
        setAlwaysOnTop(true);
        setLayout(new BorderLayout());
        setMinimumSize(new Dimension(220, 50));
        add(contentPanel, BorderLayout.CENTER);
        
        contentPanel.setBorder(new EtchedBorder());
        contentPanel.add(controlsPanel, BorderLayout.WEST);
        contentPanel.add(outerTimerPanel, BorderLayout.CENTER);
        
        controlsPanel.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
        controlsPanel.add(buttonPanel, BorderLayout.NORTH);
        
//        buttonPanel.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
        buttonPanel.add(hideButton);
//        buttonPanel.add(biggerButton);
//        buttonPanel.add(smallerButton);
//        buttonPanel.add(clearButton);

        hideButton.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
        hideButton.setBorder(new EmptyBorder(0, 3, 0, 3));
        hideButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TimerWindow.this.mainFrame.setTimerVisible(TimerWindow.this, false);
            }
        });
        biggerButton.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
        biggerButton.setBorder(new EmptyBorder(0, 3, 0, 3));
        biggerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setSize(getSize().width + 20, getSize().height + 20);
            }
        });
        smallerButton.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
        smallerButton.setBorder(new EmptyBorder(0, 3, 0, 3));
        smallerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setSize(getSize().width - 20, getSize().height - 20);
            }
        });
        clearButton.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
        clearButton.setBorder(new EmptyBorder(0, 3, 0, 3));
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //
            }
        });
        
        outerTimerPanel.setInsets(new Insets(0, 0, 0, 0));
        
        pack();
        
        controlsPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent arg0) {
                controlsPanel.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
                hideButton.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
                biggerButton.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
                smallerButton.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
                clearButton.setBackground(CONTROLS_PANEL_COLOR_PASSIV);
            }
            @Override
            public void mouseEntered(MouseEvent arg0) {
                controlsPanel.setBackground(CONTROLS_PANEL_COLOR_ACTIVE);
                hideButton.setBackground(CONTROLS_PANEL_COLOR_ACTIVE);
                biggerButton.setBackground(CONTROLS_PANEL_COLOR_ACTIVE);
                smallerButton.setBackground(CONTROLS_PANEL_COLOR_ACTIVE);
                clearButton.setBackground(CONTROLS_PANEL_COLOR_ACTIVE);
            }
            public void mousePressed(MouseEvent e) {
                currentX = e.getX();
                currentY = e.getY();
            }
        });
        controlsPanel.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseMoved(MouseEvent arg0) {
            }
            @Override
            public void mouseDragged(MouseEvent e) {
                int moveX = e.getX() - currentX;
                int moveY = e.getY() - currentY;
                
                setLocation(getLocation().x + moveX, getLocation().y + moveY);
            }
        });
    }
    
    public TimerPanel triggerTimer(TriggerEntry entry) {
        TimerPanel timerPanel = getTimerByTrigger(entry);
        if (timerPanel == null) {
            timerPanel = new TimerPanel(this, entry);
            outerTimerPanel.addLastComponent(timerPanel);
            setTimerBarStretch();
            pack();
            if (visibleIfTimer && !isVisible()) {
                setVisible(true);
            }
        }
        
        return timerPanel;
    }
    
    public void removeTimer(TimerPanel timerPanel) {
        outerTimerPanel.remove(timerPanel);
        setTimerBarStretch();
        pack();
        if (outerTimerPanel.getComponentCount() == 0) {
            if (isVisible()) {
                setVisible(false);
            }
        }
    }
    
    public void removeTimerByTrigger(TriggerEntry trigger) {
        TimerPanel timerPanel = getTimerByTrigger(trigger);
        if (timerPanel != null) {
            removeTimer(timerPanel);
        }
    }
    
    public TimerPanel getTimerByTrigger(TriggerEntry entry) {
        for (int i = 0; i < outerTimerPanel.getComponentCount(); i++) {
            TimerPanel timerPanel = (TimerPanel)outerTimerPanel.getComponent(i);
            if (timerPanel.getTriggerEntry().equals(entry)) {
                return timerPanel;
            }
        }
        return null;
    }
    
    private void setTimerBarStretch() {
        int biggestPeriod = 0;
        for (int i = 0; i < outerTimerPanel.getComponentCount(); i++) {
            TimerPanel timerPanel = (TimerPanel)outerTimerPanel.getComponent(i);
            int timerPeriod = timerPanel.getTriggerEntry().getTimerPeriod();
            if (timerPeriod > biggestPeriod) {
                biggestPeriod = timerPeriod;
            }
        }
        for (int i = 0; i < outerTimerPanel.getComponentCount(); i++) {
            TimerPanel timerPanel = (TimerPanel)outerTimerPanel.getComponent(i);
            float stretch = timerPanel.getTriggerEntry().getTimerPeriod();
            stretch /= biggestPeriod;
            timerPanel.getTimerBar().setStretch(stretch);
        }
    }
    
    public MainFrame getMainFrame() {
        return mainFrame;
    }
    
    public void setVisibleIfTimer(boolean b) {
        this.visibleIfTimer = b;
        
        if (!b && isVisible()) {
            setVisible(false);
        }
        
        if (b && outerTimerPanel.getComponentCount() > 0) {
//        if (b && outerTimerPanel.getComponentCount() > -1) {
            if (!isVisible()) {
                setVisible(true);
            }
        }
    }
    
    public boolean isVisibleIfTimer() {
        return visibleIfTimer;
    }
    
    @Override
    protected void loadSettings() {
        setSize(400, 150);
        setLocation(20 + (getWindowCount() - 1) * 300, 200);
        super.loadSettings();
        visibleIfTimer = getPrefNode().getBoolean(WINDOW_IS_VISIBLE, true);
    }
    
    @Override
    public void saveSettings() {
        super.saveSettings();
        getPrefNode().putBoolean(WINDOW_IS_VISIBLE, visibleIfTimer);
    }
    
}
